package com.alisls.shequtao.service.goods.web;

import com.alisls.shequtao.common.model.result.PageResult;
import com.alisls.shequtao.service.goods.dto.BrandDTO;
import com.alisls.shequtao.service.goods.dto.CategoryDTO;
import com.alisls.shequtao.service.goods.query.BrandPageQuery;
import com.alisls.shequtao.service.goods.service.BrandService;
import com.alisls.shequtao.service.goods.vo.BrandVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 品牌管理
 *
 * @author Ke Wang
 */
@Api(tags = "品牌管理")
@RestController
@RequestMapping("/brand")
@AllArgsConstructor
@Slf4j
public class BrandController {

    private final BrandService brandService;

    /**
     * 根据id查询品牌
     * @param id ID
     * @return 品牌对象
     */
    @ApiOperation(value = "根据id查询品牌")
    @GetMapping("/getBrandById/{id}")
    public ResponseEntity<BrandDTO> getBrandById(@PathVariable Long id) {
        return ResponseEntity.ok(brandService.convertToDTO(brandService.getById(id)));
    }

    /**
     * 分页查询品牌
     * @param brandPageQuery 分页查询参数
     * @return 品牌分页数据
     */
    @ApiOperation(value = "分页查询品牌")
    @GetMapping("/listBrandByPage")
    public ResponseEntity<PageResult<BrandDTO>> listBrandByPage(BrandPageQuery brandPageQuery) {
        PageResult<BrandDTO> pageResult = brandService.listBrandByPage(brandPageQuery);
        return ResponseEntity.ok(pageResult);
    }

    /**
     * 查询当前分类下的所有品牌
     * @param categoryId 商品分类ID
     * @return 品牌集合
     */
    @ApiOperation(value = "查询当前分类下的所有品牌")
    @GetMapping("/listBrandsByCategoryId/{categoryId}")
    public ResponseEntity<List<BrandDTO>> listBrandsByCategoryId(@PathVariable Long categoryId) {
        return ResponseEntity.ok(brandService.listBrandsByCategoryId(categoryId));
    }

    /**
     * 保存品牌
     * @param brandDTO 品牌DTO
     * @return 品牌DTO
     */
    @ApiOperation(value = "保存品牌")
    @PostMapping("/save")
    public ResponseEntity<BrandDTO> save(@RequestBody @Validated BrandDTO brandDTO) {
        brandService.save(brandDTO);
        return ResponseEntity.status(HttpStatus.CREATED)
                .body(brandDTO);
    }

    /**
     * 保存品牌（品牌带有商品分类）
     * @param brandVO 品牌VO
     * @return 品牌对象
     */
    @ApiOperation(value = "保存品牌（品牌带有商品分类）")
    @PostMapping("/saveWithCategory")
    public ResponseEntity<BrandDTO> saveWithCategory(@RequestBody @Validated BrandVO brandVO) {
        CategoryDTO categoryDTO = new CategoryDTO();
        categoryDTO.setId(brandVO.getCategoryIds()[brandVO.getCategoryIds().length - 1]);

        BrandDTO brandDTO = new BrandDTO();
        BeanUtils.copyProperties(brandVO, brandDTO);
        // TODO 品牌名称判重
        // 保存商品分类和品牌
        brandService.saveCategoryAndBrand(categoryDTO, brandDTO);
        return ResponseEntity.ok(brandDTO);
    }

}
